package com.example.administrator.about_huanxin.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.ShareprefenceUtils;
import com.example.administrator.about_huanxin.activity.LoginActivity;
import com.example.administrator.about_huanxin.activity.RegisiterActivity;
import com.example.administrator.about_huanxin.presenter.fragment.GerenPresener;
import com.example.administrator.about_huanxin.presenter.fragment.GerenPresenerImp;
import com.example.administrator.about_huanxin.view.fragment.GerenFragmentView;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/8.
 */
public class GerenFragment extends BaseFragment implements GerenFragmentView {

    @Bind(R.id.sf_img)
    ImageView sfImg;
    @Bind(R.id.self_user_tv)
    TextView selfUserTv;
    @Bind(R.id.self_btn)
    Button selfBtn;

    private GerenPresener gerenPresener;

    private Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    showToast("退出成功");
                    break;

                case 1:
                    showToast("退出失败");
                    break;
            }
        }
    };

    private void showToast(String s) {
        Toast.makeText(getActivity(),s,Toast.LENGTH_SHORT).show();
    }


    @Override
    public void setEmptyView(TextView tv, boolean isVisible) {
        tv.setText("可以让附近的人发现，空的");
        tv.setVisibility(View.GONE);
    }

    @Override
    public void setTitleText(TextView tv) {
        tv.setText("个人");
    }

    @Override
    public void setTitleButton(Button btn) {
        btn.setVisibility(View.GONE);
    }

    @Override
    public View setContainer(LayoutInflater inflater, FrameLayout fram) {
        View view = inflater.inflate(R.layout.fragment_geren, fram, true);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initContent(View content) {
        String url = ShareprefenceUtils.getResult(getActivity(), "url", "");
        String name = ShareprefenceUtils.getResult(getActivity(), "nick", "");
        Picasso.with(getActivity()).load(url).into(sfImg);
        if (!TextUtils.isEmpty(name)) {
            selfUserTv.setText(name + "");
        }else{
            selfUserTv.setText("admin");
        }

        gerenPresener = new GerenPresenerImp(this);

        selfBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gerenPresener.logoutHx();
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void logoutListener(boolean bool) {
        Message msg = Message.obtain();
        if (bool) {
            msg.what = 0;
            ShareprefenceUtils.setResult(getActivity(), "user", "");
            ShareprefenceUtils.setResult(getActivity(), "pwd", "");
            ShareprefenceUtils.setResult(getActivity(), "nick", "");
            ShareprefenceUtils.setResult(getActivity(), "url", "");
            startActivity(new Intent(getActivity(), RegisiterActivity.class));
            getActivity().finish();
        }else{
            msg.what = 1;
        }
        h.sendMessage(msg);
    }
}
